package com.vaccine.controller;

import com.vaccine.common.Result;
import com.vaccine.entity.Vaccine;
import com.vaccine.service.VaccineService;
import com.vaccine.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vaccine")
public class VaccineController {
    @Autowired
    private VaccineService vaccineService;

    @PostMapping("/insert")
    public Result<?> insert(@RequestBody Vaccine vaccine) {
        try {
            vaccineService.insert(vaccine);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("0", "新增失败！");
        }

    }

    @PutMapping("/update")
    public Result<?> update(@RequestBody Vaccine vaccine) {
        try {
            vaccineService.update(vaccine);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("0", "修改失败");
        }
    }

    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam("id") Integer id) {
        try {
            vaccineService.delete(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("0", "删除失败");
        }
    }

    @GetMapping("/list")
    public Result<?> getByPage(PageInfo pageInfo) {
        pageInfo = vaccineService.getByPage(pageInfo);
        return Result.success(pageInfo);
    }

    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        try {
            vaccineService.deleteBatch(ids);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("0", "批量删除失败");
        }
    }

    @GetMapping("/getVaccinePlace")
    public Result<?> getVaccinePlace(@RequestParam("id") Integer id) {
        String[] placeArray = vaccineService.vaccinePlace(id);
        return new Result<>("200", "获取成功", placeArray);
    }


}
